package com.sjy.MyBlog.web;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.github.pagehelper.PageInfo;
import com.sjy.MyBlog.bean.ArticleWithBLOBs;
import com.sjy.MyBlog.biz.impl.ArticleBizImpl;

@Controller
@SessionAttributes({"currentPage","pageNum"}) // pageNum 当前页
public class ListAction {

	@Resource
	private ArticleBizImpl ab;
	
	@GetMapping("toList.do")
	public String toList(Model m,@SessionAttribute("currentPage") String currentPage ) {
		currentPage="blog-list::listBody";
		m.addAttribute("currentPage", currentPage);
		PageInfo<ArticleWithBLOBs> articlePages = new PageInfo<ArticleWithBLOBs>(ab.selectArticleByPage(1, 5));
		m.addAttribute("articlePages", articlePages);
		m.addAttribute("pageNum", 1);
		return currentPage;
	}
	
	@GetMapping("nextPage.do")
	public String nextPage(Model m,
			@SessionAttribute("currentPage") String currentPage,
			@SessionAttribute("pageNum") int pageNum) {
		pageNum = pageNum + 1;
		
		PageInfo<ArticleWithBLOBs> articlePages = new PageInfo<ArticleWithBLOBs>(ab.selectArticleByPage(pageNum, 1));
		m.addAttribute("articlePages", articlePages);
		
		m.addAttribute("pageNum", pageNum);
		
		
		return currentPage;
	}
	
	@GetMapping("prePage.do")
	public String prePage(Model m,
			@SessionAttribute("currentPage") String currentPage,
			@SessionAttribute("pageNum") int pageNum) {
		pageNum = pageNum - 1;
		
		PageInfo<ArticleWithBLOBs> articlePages = new PageInfo<ArticleWithBLOBs>(ab.selectArticleByPage(pageNum, 1));
		m.addAttribute("articlePages", articlePages);
		
		m.addAttribute("pageNum", pageNum);
		
		return currentPage;
	}
	
}
